//
//  BookCommentViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-10.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "BookCommentViewController.h"
#import "BookCommentCell.h"

@interface BookCommentViewController ()

@end

@implementation BookCommentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotBookComment:) name:gotBookCommentNotify object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateComment) name:kUpdateComment object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    commentTableView.hidden = YES;
    nothingView.hidden = YES;
    [self showLoadingView];
    [[DataRequest sharedRequest] getBookCommentByID:self.itemid];
}

- (void)gotBookComment:(NSNotification *)notification
{
    [self removeloadingView];
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSArray *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSArray class]]) {
            self.commentArray = result;
            [commentTableView reloadData];
            [commentTableView setContentOffset:CGPointMake(0, 0) animated:YES];
            if (_commentArray.count == 0) {
                commentTableView.hidden = YES;
                nothingView.hidden = NO;
            }
            else {
                commentTableView.hidden = NO;
                nothingView.hidden = YES;
            }
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookCommentCell *cell = [commentTableView dequeueReusableCellWithIdentifier:@"BookCommentCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BookCommentCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.commentInfo = [self.commentArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *commentInfo = [self.commentArray objectAtIndex:indexPath.row];
    NSString *comment = [commentInfo valueForKey:@"content"];
    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(300, 99999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"heightForRowAtIndexPath>>>>%@",NSStringFromCGSize(size));
    return 50 + size.height;
}

- (void)updateComment
{
    [self showLoadingView];
    [[DataRequest sharedRequest] getBookCommentByID:self.itemid];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:self.view.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [commentTableView release];
    [_itemid release];
    [_commentArray release];
    [nothingView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [commentTableView release];
    commentTableView = nil;
    [nothingView release];
    nothingView = nil;
    [super viewDidUnload];
}
@end
